package services

import (
	"errors"
	"fmt"
	"hft/Database/admin"
	"hft/Http/Models/dao"
	"hft/Http/Models/request"
	"hft/Http/Services/base"
	"hft/Http/Services/front/pc/app_utils"
	utils "hft/Http/Utils"
	"hft/global"
	"hft/tools"

	"gorm.io/gorm"
)

type Article struct{}

func (*Article) CreateArticle(claims tools.BaseClaims, article *request.ArticleReq) (err error) {

	var account *admin.Account

	var group *admin.ArticleType

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return errors.New("无该账号信息")
	}

	err = global.OC_DB.First(&group, article.ArtType).Error

	if err != nil {
		return errors.New("分组不存在")
	}

	// if group.AccountId != account.ID {
	// 	return errors.New("你不可能在此分组中添加或修改文章")
	// }
	pureText, _ := utils.ConvertHTMLToPlainText(app_utils.TruncateString(article.Cont, 200))
	err = global.OC_DB.Create(&admin.ArticleBase{
		RegionLink:  account.RegionLink,
		HasImg:      article.HasImg,
		HasVideo:    article.HasVideo,
		Name:        article.Name,
		Cover:       article.Cover,
		ArtType:     article.ArtType,
		Author:      article.Author,
		AccountId:   account.ID,
		AccountLink: fmt.Sprintf("%v%v,", account.AccountLink, account.ID),
		Cont:        &admin.ArticleCont{Content: article.Cont},
		PureText:    pureText,
	}).Error

	if err != nil {
		return errors.New("未知错误500")
	}

	return nil
}

func (*Article) EditArticle(claims tools.BaseClaims, article *request.ArticleReq) (err error) {
	var account *admin.Account

	var group *admin.ArticleType

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return errors.New("无该账号信息")
	}

	err = global.OC_DB.First(&group, article.ArtType).Error

	if err != nil {
		return errors.New("分组不存在")
	}

	err = global.OC_DB.Model(&admin.ArticleBase{}).Where("id = ?", article.ID).Updates(&admin.ArticleBase{
		HasImg:   article.HasImg,
		HasVideo: article.HasVideo,
		Name:     article.Name,
		Cover:    article.Cover,
		ArtType:  article.ArtType,
		Author:   article.Author,
	}).Error

	if err != nil {
		return errors.New("未知错误500")
	}

	err = global.OC_DB.Model(&admin.ArticleCont{}).Where("article_id = ?", article.ID).Update("content", article.Cont).Error

	if err != nil {
		return errors.New("未知错误500")
	}

	return nil
}

func (*Article) GetList(claims tools.BaseClaims, paginate *request.ArtSearchReq) (data *utils.Page, err error) {

	var articles []*admin.ArticleBase

	account, err := base.FindAccount(claims, global.OC_DB)
	if err != nil {
		return nil, err
	}

	global.OC_DB.Scopes(dao.GetArticleDao.GetArticleList(account.RegionLink, paginate, global.OC_DB)).Find(&articles).Offset(-1).Limit(-1).Count(&paginate.Total)

	for _, v := range articles {
		v.Cover = global.OC_CONFIG.AliyunOSS.BucketUrl + v.Cover
	}

	paginate.Data = articles

	return &paginate.Page, nil
}

func (*Article) GetArticle(claims tools.BaseClaims, id int) (article *admin.ArticleBase, err error) {

	var account *admin.Account

	var search *admin.ArticleBase

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return nil, errors.New("无该账号信息")
	}

	err = global.OC_DB.Omit("account_link").Preload("Cont", func(db *gorm.DB) *gorm.DB {
		return db.Omit("id,update_time,create_time")
	}).First(&search, id).Error

	if err != nil {
		return nil, errors.New("找不到文章信息")
	}
	return search, nil
}

func (*Article) DeleteArt(claims tools.BaseClaims, ids []int) (err error) {

	var account *admin.Account

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return errors.New("找不到该账号信息")
	}

	var list []*admin.ArticleBase

	global.OC_DB.Find(&list, ids)

	res := utils.IsThisArtBelongThisAccount(list, account.RegionLink)

	if !res {
		return errors.New("你无权删除该文章")
	}

	global.OC_DB.Model(&admin.ArticleBase{}).Unscoped().Delete(&list)

	return nil
}

var ArticleService = new(Article)
